package com.coolingme.interceptor;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * 自定义分区策略
 *
 * @author wangyue
 * @date 2023/5/12 14:11
 */
public class MyPartitioner implements Partitioner {

    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        Integer keyInteger = Integer.valueOf(key.toString());
        if (keyInteger <= 5) {
            // 发送到分区0
            return 0;
        } else {
            // 发送到分区1，如果没有分区1，则发送失败
            return 1;
        }
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> configs) {

    }
}
